package com.jboa.service.impl;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.jboa.dao.IBizClaimVoucherDAO;
import com.jboa.dao.IEmployeeDAO;
import com.jboa.dao.ILeaveDAO;
import com.jboa.dao.IResultDAO;
import com.jboa.service.ICheckService;
import com.jboa.vo.BizCheckResult;
import com.jboa.vo.BizLeave;
import com.jboa.vo.SysEmployee;

@Service("checkService")
public class CheckServiceImpl implements ICheckService {
	@Autowired
	@Qualifier("employeeDao")
	private IEmployeeDAO empdao;
	@Autowired
	@Qualifier("voucherDao")
	private IBizClaimVoucherDAO voucherdao;
	@Autowired
	@Qualifier("resultDao")
	private IResultDAO resultdao;
	@Autowired
	@Qualifier("Leavedao")
	private ILeaveDAO leavedao;

	/**
	 * 部门经理审核报销单
	 */
	public boolean deptManagerCheckVoucher(BizCheckResult result) {
		try {
			// 结果为通过，判断报销金额>=5000--待审核，否则--财务
			if (result.getResult().equals("通过")) {
				// 判断金额 大于等于5000
				if (result.getBizClaimVoucher().getTotalAccount() >= 5000) {
					result.getBizClaimVoucher().setStatus("待审批");
					// 获取该部门总经理
					SysEmployee emp = empdao.getByDeptAndPosition("总经理", result
							.getBizClaimVoucher().getSysEmployeeByCreateSn()
							.getSysDepartment());
					result.getBizClaimVoucher().setSysEmployeeByNextDealSn(emp);
				}
				// 小于5000
				else {
					result.getBizClaimVoucher().setStatus("已审批");
					// 获取该部门财务
					SysEmployee emp = empdao.getByDeptAndPosition("财务", result
							.getBizClaimVoucher().getSysEmployeeByCreateSn()
							.getSysDepartment());
					result.getBizClaimVoucher().setSysEmployeeByNextDealSn(emp);
				}
			}
			// 结果为拒绝，状态--已终止
			else if (result.getResult().equals("拒绝")) {
				result.getBizClaimVoucher().setStatus("已终止");
				result.getBizClaimVoucher().setSysEmployeeByNextDealSn(null);
			}
			// 结果为打回，改--填报人
			else if (result.getResult().equals("打回")) {
				result.getBizClaimVoucher().setStatus("已打回");
				result.getBizClaimVoucher().setSysEmployeeByNextDealSn(
						result.getBizClaimVoucher().getSysEmployeeByCreateSn());
			}
			// 更新报销单
			voucherdao.update(result.getBizClaimVoucher());
			// 保存审核结果
			resultdao.save(result);
			if (result.getId() != null) {
				return true;
			} else {
				return false;
			}
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("出错了");
			return false;
		}

	}

	/**
	 * 总经理审核报销单
	 */
	public boolean ManagerCheckVoucher(BizCheckResult result) {
		try {
			// 结果为通过提交给财务
			if (result.getResult().equals("通过")) {
				result.getBizClaimVoucher().setStatus("已审批");
				// 获取该部门财务
				SysEmployee emp = empdao.getByDeptAndPosition("财务", result
						.getBizClaimVoucher().getSysEmployeeByCreateSn()
						.getSysDepartment());
				result.getBizClaimVoucher().setSysEmployeeByNextDealSn(emp);
			}
			// 结果为拒绝，状态--已终止
			else if (result.getResult().equals("拒绝")) {
				result.getBizClaimVoucher().setStatus("已终止");
				result.getBizClaimVoucher().setSysEmployeeByNextDealSn(null);
			}
			// 结果为打回，改--填报人
			else if (result.getResult().equals("打回")) {
				result.getBizClaimVoucher().setStatus("已打回");
				result.getBizClaimVoucher().setSysEmployeeByNextDealSn(
						result.getBizClaimVoucher().getSysEmployeeByCreateSn());
			}

			// 更新报销单
			voucherdao.update(result.getBizClaimVoucher());
			// 保存审核结果
			resultdao.save(result);
			if (result.getId() != null) {
				return true;
			} else {
				return false;
			}
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("出错了");
			return false;
		}
	}

	/**
	 * 财务处理报销单
	 */
	public boolean cashierDeal(BizCheckResult result) {

		try {
			result.getBizClaimVoucher().setStatus("已付款");
			result.getBizClaimVoucher().setSysEmployeeByNextDealSn(null);
			// 更新报销单
			voucherdao.update(result.getBizClaimVoucher());
			// 保存审核结果
			resultdao.save(result);
			if (result.getId() != null) {
				return true;
			} else {
				return false;
			}
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("出错了");
			return false;
		}
	}

	/**
	 * 部门经理审批请假单
	 */
	public boolean deptCheckLeave(BizLeave leave) {
		BizLeave leavedb=leavedao.getByid(leave.getId());
		leavedb.setApproveOpinion(leave.getApproveOpinion());
		leavedb.setStatus(leave.getStatus());
		leavedb.setModifytime(new Date());
		leavedb.setNextDealSn(null);
		//更新请假单
		try{
			leavedao.updateLeave(leavedb);
			return true;
		}catch(Exception e){
			e.printStackTrace();
			System.out.println("出错了");
			return false;
		}
		
	}

}
